** This file produces Figure 4.4
use npat1996_2008.dta, clear

* Drop any values that are missing
drop if np_score==.

* Generate separate variables for ideology for Republicans and Democrats
gen repideo=np_score if party=="R"
gen demideo=np_score if party=="D"

* Generate variables indicating bounds of ideology among republicans and democrats
bysort year statabbr chamber party: egen min_r_ideo=min(repideo)
bysort year statabbr chamber party: egen max_d_ideo=max(demideo)

* Fill in missing years with previous year value
sort year statabbr chamber min_r_ideo
replace min_r_ideo=min_r_ideo[_n-1] if min_r_ideo==. & statabbr==statabbr[_n-1] & year==year[_n-1] & chamber==chamber[_n-1]
sort year statabbr chamber max_d_ideo
replace max_d_ideo=max_d_ideo[_n-1] if max_d_ideo==. & statabbr==statabbr[_n-1] & year==year[_n-1] & chamber==chamber[_n-1]

* Generate an indicator for whether a legislator overlaps with the most conservative
* Dem or most liberal Rep
gen bipartisan=1 if np_score>min_r_ideo & party=="D"
replace bipartisan=1 if np_score<max_d_ideo & party=="R"

* Create column of ones to add for total legislators
gen total=1

* Collapse data to chamber/year 
collapse (sum) total bipartisan, by(year statabbr chamber)

* Create variable for percent of legislators overlapping with other party's most
* moderate member
gen percent_bipartisan=bipartisan/total

* Merge in dataset of campaign finance laws
sort statabbr year
merge statabbr year using masterdataset
drop if _m==2
drop _m

* Generate "party limits" variable and label it
gen partylims=1 if l_party_cand==1 | l_ind_party==1
replace partylims=0 if l_party_cand==0 & l_ind_party==0
label define limits 0 "Unlimited" 1 "Limited"
label values partylims limits

* Fill in values for laws
replace partylims=partylims[_n-1] if partylims==. & statabbr==statabbr[_n-1]
replace orglims=orglims[_n-1] if orglims==. & statabbr==statabbr[_n-1]
replace indlims=indlims[_n-1] if indlims==. & statabbr==statabbr[_n-1]

* Estimate regression model
areg percent_bipartisan partylims orglims indlims , absorb(statabbr)

* Produce bar graph for Figure 4.4
graph bar percent_bipartisan, over(partylims) scheme(s1mono) ylabel(, nogrid) ytitle("Proportion")
graph export figure4_4.eps, replace


